extends Node2D
class_name BattleActor

@export var id = "yamu"
@export var testMode = false

@onready var animationPlayer: AnimationPlayer = $AnimationPlayer
@onready var animatedSprite2d: AnimatedSprite2D = $AnimatedSprite2D

var data
var animPaused = false

signal sigTakeEffect
signal sigPauseAnim


func _ready() -> void:
	animationPlayer.stop()
	
	var newMaterial = ShaderMaterial.new()
	newMaterial.shader = ResourcePreload.resources["ActorHitShader"]
	animatedSprite2d.material = newMaterial
	

func init(_data):
	data = _data
	id = data.id
	
	initSpriteFrames()
	animatedSprite2d.play("idle")	
	
	
func initSpriteFrames():
	animatedSprite2d.sprite_frames = ResourcePreload.resources["ba_" + id]
	

func _physics_process(delta: float) -> void:
	handleTest()


func handleTest():
	if not testMode: return
	
	if Input.is_action_just_pressed("DoTest"):
		animationPlayer.play(id + "_attack")
		

func triggerTakeEffect():
	sigTakeEffect.emit()
	

func triggerPauseAnim():
	sigPauseAnim.emit()
	
	
func playAnim(animName):
	animationPlayer.play(id + "_" + animName)
	

func pauseAnim():
	animationPlayer.pause()
	animPaused = true
	

func resumeAnim():
	animationPlayer.play()
	animPaused = false
	

func isAnimPaused():
	return animPaused


func isAnimPlaying():
	return animationPlayer.is_playing()
	

func setHitEffect():
	animatedSprite2d.material.set_shader_parameter("hitStrength", 0.7)
	

func unsetHitEffect():
	animatedSprite2d.material.set_shader_parameter("hitStrength", 0)
